package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.SecurityUtils;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallExpressTemplate;
import com.tencreat.domain.mall.MallExpressZone;
import com.tencreat.service.mall.IMallExpressZoneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 快递专区Controller
 *
 * @author tencreat
 * @date 2022-01-06
 */
@Api(tags="快递专区api")
@RestController
@RequestMapping("/system/zone")
public class AdminMallExpressZoneController extends BaseController {
    @Autowired
    private IMallExpressZoneService mallExpressZoneService;

    /**
     * 查询快递专区列表
     */
    @ApiOperation(value="查询快递专区列表")
    @PreAuthorize("@ss.hasPermi('system:zone:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallExpressZone>> list(@RequestBody  MallExpressZone mallExpressZone) {
        QueryWrapper<MallExpressZone> wrapper = Wrappers.query(mallExpressZone);
        if(null == mallExpressZone.getTemplateId()){
            wrapper.lambda().and(wrapper1 -> wrapper1.isNull(MallExpressZone::getTemplateId));
        }
        if (SecurityUtils.getLoginUser().getUser().getUserId() != 1 && SecurityUtils.getLoginUser().getUser().getUserId() !=2) {
            wrapper.lambda().and(wrapper1 -> wrapper1.eq(MallExpressZone::getUserId, SecurityUtils.getLoginUser().getUser().getUserId()));
        }
        Page<MallExpressZone> page =  new Page<>(mallExpressZone.getPageNum(), mallExpressZone.getPageSize());
        return HttpResult.ok(mallExpressZoneService.page(page, wrapper));
    }
    /**
     * 查询快递专区列表
     */
    @ApiOperation(value="查询快递专区列表")
    @PostMapping("/allList")
    public HttpResult<List<MallExpressZone>> allList(@RequestBody  MallExpressZone mallExpressZone) {
        List<MallExpressZone> list = mallExpressZoneService.lambdaQuery().eq(MallExpressZone::getTemplateId, mallExpressZone.getTemplateId()).list();
        return HttpResult.ok(list);
    }

    /**
     * 导出快递专区列表
     */
    @ApiOperation(value="导出快递专区列表")
    @PreAuthorize("@ss.hasPermi('system:zone:export')")
    @Log(title = "基础配置>快递配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallExpressZone mallExpressZone) {
        List<MallExpressZone> list = mallExpressZoneService.selectMallExpressZoneList(mallExpressZone);
        ExcelUtil<MallExpressZone> util = new ExcelUtil<MallExpressZone>(MallExpressZone.class);
        return util.exportExcel(list, "zone");
    }

    /**
     * 获取快递专区详细信息
     */
    @ApiOperation(value="获取快递专区详细信息")
    @PreAuthorize("@ss.hasPermi('system:zone:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallExpressZone> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallExpressZoneService.selectMallExpressZoneById(id));
    }

    /**
     * 新增快递专区
     */
    @ApiOperation(value="新增快递专区")
    @PreAuthorize("@ss.hasPermi('system:zone:add')")
    @Log(title = "基础配置>快递配置", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallExpressZone mallExpressZone) {
        return to(mallExpressZoneService.insertMallExpressZone(mallExpressZone));
    }

    /**
     * 修改快递专区
     */
    @ApiOperation(value="修改快递专区")
    @PreAuthorize("@ss.hasPermi('system:zone:edit')")
    @Log(title = "基础配置>快递配置", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallExpressZone mallExpressZone) {
        return to(mallExpressZoneService.updateMallExpressZone(mallExpressZone));
    }

    /**
     * 删除快递专区
     */
    @ApiOperation(value="删除快递专区")
    @PreAuthorize("@ss.hasPermi('system:zone:remove')")
    @Log(title = "基础配置>快递配置", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallExpressZoneService.deleteMallExpressZoneByIds(ids));
    }

    /**
     * 获取当前模板已经选中过得省份
     */
    @ApiOperation(value="获取当前模板已经选中过得省份")
    @Log(title = "快递专区", businessType = BusinessType.DELETE)
    @PostMapping("/getUseredProvince")
    public HttpResult<List<Long>> getUseredProvince(@RequestBody  ReqIdsParam ids) {
        return HttpResult.ok((mallExpressZoneService.getUseredProvince(ids)));
    }
}
